package org.edu.modules.growth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.api.client.util.Lists;
import com.google.api.client.util.Maps;
import lombok.AllArgsConstructor;
import org.edu.modules.growth.config.EvaluateResultTypeEnum;
import org.edu.modules.growth.config.QueryConditionTool;
import org.edu.modules.growth.entity.*;
import org.edu.modules.growth.entity.bo.VeBaseDeptBO;
import org.edu.modules.growth.mapper.*;
import org.edu.modules.growth.service.IVeBaseManageService;
import org.edu.modules.growth.service.IVeJsczEvaluateService;
import org.edu.modules.growth.vo.QueryCondition;
import org.edu.modules.growth.vo.TeacherBaseInfoVo;
import org.edu.modules.growth.vo.VeJsczEvaluateExcelVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class VeJsczEvaluateServiceImpl extends ServiceImpl<IVeJsczEvaluateMapper,
        VeJsczEvaluate> implements IVeJsczEvaluateService {

    private IVeJsczEvaluateMapper evaluateMapper ;

    private IVeJsczAwardsMapper mAwardsMapper ;

    private IVeJsczThieseMapper mThieseMapper ;

    private IVeJsczLearntrainingMapper mLearntrainingMapper ;

    private IVeJsczTraingMapper mTraingMapper ;

    private IVeJsczClassRoomMapper mClassRoomMapper ;

    private IVeJsczResearcherMapper mResearcherMapper ;

    private IVeJsczAccidentMapper mAccidentMapper ;

    private IVeJsczUateMapper mUateMapper ;

    private IVeJsczExamMapper mExamMapper ;

    private IVeBaseManageService mIVeBaseManageService ;


    @Override
    public Map<String,Object> selectPage(QueryCondition queryCondition ){
        HashMap<String,Object> map = new HashMap<>();
        IPage<VeJsczEvaluate> page = new Page<>(queryCondition.getCurrent(), queryCondition.getPageSize());
        //创建查询的条件
        QueryWrapper<VeJsczEvaluate> wrapper = new QueryWrapper<>() ;
        QueryConditionTool.advancedQueryPage(queryCondition.getConditions(),wrapper);
        wrapper.orderByAsc("id");
        page = evaluateMapper.selectPage(page,wrapper);
        List<VeJsczEvaluate> list = page.getRecords();
        Integer count = evaluateMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count", count);
        return map;
    }

    @Override
    public TeacherBaseInfoVo getInfo(Long teacherUserId,String yearCode) {
        TeacherBaseInfoVo vo = new TeacherBaseInfoVo() ;
        // 获奖数
        QueryWrapper<VeJsczAwards> wrapperAward = new QueryWrapper<>() ;
        wrapperAward.eq("teacherUserId",teacherUserId);
        int awardNum = mAwardsMapper.selectCount(wrapperAward) ;
        // 论文数
        QueryWrapper<VeJsczThiese> wrapper2 = new QueryWrapper<>() ;
        wrapper2.eq("teacherUserId",teacherUserId);
        int thesisNum = mThieseMapper.selectCount(wrapper2) ;
        // 培训经历
        QueryWrapper<VeJsczLearntraining> wrapper3 = new QueryWrapper<>() ;
        wrapper3.eq("teacherUserId",teacherUserId);
        int num1 = mLearntrainingMapper.selectCount(wrapper3) ;
        QueryWrapper<VeJsczTraining> wrapper4 = new QueryWrapper<>() ;
        wrapper4.eq("teacherUserId",teacherUserId);
        int num2 = mTraingMapper.selectCount(wrapper4) ;
        Integer trainNum = num1 + num2 ;
        // 任课数
        QueryWrapper<VeJsczClassRoom> wrapper5 = new QueryWrapper<>() ;
        wrapper5.eq("teacherUserId",teacherUserId);
        int courseNum = mClassRoomMapper.selectCount(wrapper5) ;
        // 科研项目数
        QueryWrapper<VeJsczResearcher> wrapper6 = new QueryWrapper<>() ;
        wrapper6.eq("chargeUid", teacherUserId) ;
        int researchNum = mResearcherMapper.selectCount(wrapper6) ;
        // 教学事故
        QueryWrapper<VeJsczAccident> wrapper7 = new QueryWrapper<>() ;
        wrapper7.eq("teacherUserId", teacherUserId) ;
        wrapper7.orderByDesc("accidentTime") ;
        List<VeJsczAccident> accidents = mAccidentMapper.selectList(wrapper7) ;
        // 期初考核
        QueryWrapper<VeJsczUate> wrapper8 = new QueryWrapper<>() ;
        wrapper8.eq("teacherUserId", teacherUserId) ;
        wrapper8.eq("type", "QC") ;
        wrapper8.eq("yearCode", yearCode) ;
        wrapper8.orderByDesc("createTime") ;
        List<VeJsczUate> QC = mUateMapper.selectList(wrapper8) ;
        // 期末考核
        QueryWrapper<VeJsczUate> wrapper9 = new QueryWrapper<>() ;
        wrapper9.eq("teacherUserId", teacherUserId) ;
        wrapper9.eq("type", "QM") ;
        wrapper9.eq("yearCode", yearCode) ;
        wrapper9.orderByDesc("createTime") ;
        List<VeJsczUate> QM = mUateMapper.selectList(wrapper9) ;
        // 评教结果
        QueryWrapper<VeJsczExam> wrapper10 = new QueryWrapper<>() ;
        wrapper10.eq("teacherUserId", teacherUserId) ;
        wrapper10.eq("yearCode", yearCode) ;
        wrapper10.orderByDesc("id") ;
        List<VeJsczExam> exam = mExamMapper.selectList(wrapper10) ;

        return vo.setAccidents(accidents).setAwardNum(awardNum).setCourseNum(courseNum).setResearchNum(researchNum)
                .setTeacherUserid(teacherUserId).setQC(QC).setQM(QM).setThesisNum(thesisNum).setTrainNum(trainNum)
                .setExam(exam);
    }

    @Override
    public List<VeJsczEvaluateExcelVo> exportData(Integer yearCode, String JG, String XM, String GH) {
        List<VeJsczEvaluateExcelVo> excelList = Lists.newArrayList() ;
        QueryWrapper<VeJsczEvaluate> wrapper = new QueryWrapper<>() ;
        wrapper.eq("yearCode",yearCode);
        if( JG != null){
            wrapper.eq("result",JG);
        }
        if( XM != null){
            wrapper.like("XM",XM);
        }
        if( GH != null){
            wrapper.eq("GH",GH);
        }
        List<VeJsczEvaluate> list = evaluateMapper.selectList(wrapper) ;
        if( !CollectionUtils.isEmpty(list)){
            //获取所有部门信息
            List<VeBaseDeptBO> deptList = mIVeBaseManageService.deptAll().getResult() ;
            List<VeBaseDeptBO> dept = Lists.newArrayList() ;
            deptList.forEach(l->{
                addDept(l,dept) ;
            });
            Map<Long,String> deptMap = Maps.newHashMap() ;
            dept.forEach(l->{
                deptMap.put(l.getId(),l.getJgmc()) ;
            });
            //TODO 获取所有专业组信息
//            List<VeBaseZyzBO> zyzList = mIVeBaseManageService.zyzAll().getResult() ;
//            List<VeBaseZyzBO> zyz = Lists.newArrayList() ;
//            deptList.forEach(l->{
//                addDept(l,dept) ;
//            });
//            Map<Long,String> zyzMap = Maps.newHashMap() ;
//            zyz.forEach(l->{
//                zyzMap.put(l.getId(),l.getJgmc()) ;
//            });

            list.forEach(l->{
                VeJsczEvaluateExcelVo vo = new VeJsczEvaluateExcelVo() ;
                vo.setGH(l.getGH()).setXM(l.getXM())
                        .setResult(EvaluateResultTypeEnum.getNameByValue(l.getResult()))
                        .setYearCode(l.getYearCode())
                        .setDepName(deptMap.get(l.getDepId())) ;
//                        .setJyzName()
                excelList.add(vo) ;
            });
        }
        return excelList;
    }

    private void addDept( VeBaseDeptBO bo , List<VeBaseDeptBO> dept ){
        dept.add(bo) ;
        if( !CollectionUtils.isEmpty(bo.getChildren())){
            bo.getChildren().forEach(l->{
                addDept(l,dept) ;
            });
        }
    }


}
